package demo.zyy.com.dreamstudio.bottomnavigation;

import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import demo.zyy.com.dreamstudio.R;

/**
 * Created by Administrator on 2017/8/4.
 */

public class BottomNavigatorView extends LinearLayoutCompat {

    public interface OnBottomNavigatorViewItemClickListener{
        void OnBNViewItemClick(int postion, View view);
    }
    OnBottomNavigatorViewItemClickListener mOnBottomNavigatorViewItemClickListener;
    public BottomNavigatorView(Context context) {
        this(context,null);
    }
    public BottomNavigatorView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }
    public BottomNavigatorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(HORIZONTAL);
        inflate(context, R.layout.navigator_buttom_layout,this);
        for(int i=0;i<getChildCount();i++){
           View view=getChildAt(i);
            final int finalI=i;
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnBottomNavigatorViewItemClickListener.OnBNViewItemClick(finalI,v);
                }
            });
        }
    }
     public void select(int position){
         for(int i=0;i<getChildCount();i++){
             View child=getChildAt(i);
             if(i==position){
                 selectChild(child,true);
             }else {
                 selectChild(child,false);
             }
         }
     }
      public void selectChild(View child,boolean b){
       if(child instanceof ViewGroup){
           ViewGroup group= (ViewGroup) child;
           group.setSelected(b);
           for(int i=0;i<group.getChildCount();i++){
               selectChild(group.getChildAt(i),b);
           }
       }else {
           child.setSelected(b);
           if(child instanceof ImageView){
               ImageView view= (ImageView) child;
               Drawable drawable=view.getDrawable().mutate();
               if(b){
                   drawable.setColorFilter(getResources().getColor(R.color.f85e5b), PorterDuff.Mode.SRC_ATOP);
               }else {
                   drawable.setColorFilter(getResources().getColor(R.color.c777777), PorterDuff.Mode.SRC_ATOP);
               }

           }
       }
       }
       public void  setmOnBottomNavigatorViewItemClickListener(OnBottomNavigatorViewItemClickListener hnBottomNavigatorViewItemClickListener){
           mOnBottomNavigatorViewItemClickListener=hnBottomNavigatorViewItemClickListener;
       }

}
